package com.tjl.foodapp;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;

import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AddCommentFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AddCommentFragment extends DialogFragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public AddCommentFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment AddCommentFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static AddCommentFragment newInstance(String param1, String param2) {
        AddCommentFragment fragment = new AddCommentFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        DisplayMetrics metric = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(metric);
        int width = metric.widthPixels-50;
        int height = metric.heightPixels/4;
        getDialog().getWindow().setLayout(width,height);

    }
    TextView tx_title_pinglun;
    EditText et_content_pinglun;
    Button btn_cancel_pinglun;
    Button btn_ok_pinglun;
    int order_id;
    String type="";
    String msg = "";
    Context context;
    RequestQueue queue;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_add_comment, container, false);
        context = this.getActivity();
        queue = Volley.newRequestQueue(context);
        tx_title_pinglun = view.findViewById(R.id.tx_title_pinglun);
        et_content_pinglun = view.findViewById(R.id.et_content_pinglun);
        btn_cancel_pinglun = view.findViewById(R.id.btn_cancel_pinglun);
        btn_ok_pinglun = view.findViewById(R.id.btn_ok_pinglun);
        order_id = this.getArguments().getInt("order_id");
        type = this.getArguments().getString("type");
        if(type.equals("add"))tx_title_pinglun.setText("添加评论");
        else if(type.equals("update")){
            tx_title_pinglun.setText("修改评论");
            et_content_pinglun.setText(this.getArguments().getString("content"));
        }
        btn_cancel_pinglun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddCommentFragment.this.dismiss();
            }
        });
        btn_ok_pinglun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = et_content_pinglun.getText().toString();
               updatePinglunByURL(Utils.urlBase+"and_updatePinglun.do?id="+order_id+"&content="+content);
            }
        });
        return view;
    }
    public void updatePinglunByURL(String url){
        Response.Listener listener1 = new Response.Listener() {
            @Override
            public void onResponse(Object o) {
                try {
                    JSONObject obj = new JSONObject(o.toString());
                    msg = obj.getString("msg");
                    if(type.equals("add"))msg = "添加评论"+msg;
                    else if(type.equals("update"))msg = "评论修改"+msg;
                    Toast.makeText(context,msg,Toast.LENGTH_LONG).show();

                        MeFragment.getOrderArrayList(Utils.urlBase+"and_getOrdersByUserId.do?id="+MainActivity.user_id);
                        MeFragment.getPinglunArrayList(Utils.urlBase+"and_getPinglunByUserId.do?id="+MainActivity.user_id);

                    AddCommentFragment.this.dismiss();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
        Response.ErrorListener listener2 = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(context,"网络连接失败",Toast.LENGTH_LONG).show();
            }
        };
        JsonRequest request = new JsonObjectRequest(url,null,listener1,listener2);
        queue.add(request);
    }
}